package com.maoye.ossp.common.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.ossp.sys.site.entity.SiteNav;
import com.maoye.ossp.sys.site.entity.SiteNavMenu;
import com.maoye.ossp.sys.site.service.ISiteNavMenuService;
import com.maoye.ossp.sys.site.service.ISiteNavService;
import com.maoye.sc.api.domain.User;

/**
 * 主页Contorller
 * 
 * @author zhuangwl
 * 
 */
@Controller
public class HomeController extends BaseController {

	@Resource
	private ISiteNavMenuService siteNavMenuService;

	@Resource
	private ISiteNavService navService;
	
	/**
	 * 进入欢迎页面
	 */
	@RequestMapping(value = "/sys/home")
	public String brand(HttpServletRequest request, HttpServletResponse response) {
		return "/sys/sysHome";
	}
	
	/**
	 * 进入首页
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		// 未登录，则跳转到登录页
		if (user.getAccountId() == null) {
			return "redirect:/login";
		}
		model.addAttribute("navId", "13300000000000001");
		return "/index";
	}
	
	/**
	 * 导航
	 */
	@RequestMapping(value = "/{navId}-{code}.html")
	public String navIndex(@PathVariable String navId,
			@PathVariable String code, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String rtn = "/index";
		if (StringUtils.isNotEmpty(navId)) {
			SiteNav nav = navService.get(Long.valueOf(navId));
			rtn = "redirect:" + nav.getHref();
		}
		model.addAttribute("navId", navId);
		return rtn;
	}

	/**
	 * 菜单
	 */
	@RequestMapping(value = "/menu/{navId}-{menuId}.html")
	public String menu(@PathVariable String navId, @PathVariable String menuId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String rtn = "/index";
		
		if (StringUtils.isNotEmpty(menuId)) {
			SiteNavMenu menu = siteNavMenuService.get(Long.valueOf(menuId));
			rtn = "redirect:" + menu.getHref();
			navId = menu.getSiteNavId().toString();
		}
		model.addAttribute("navId", navId);
		return rtn;
	}

	/**
	 * 进入 系统管理首页
	 */
	@RequestMapping(value = "/sys")
	public String sysIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String navId = request.getParameter("navId");
		navId = StringUtils.isEmpty(navId) ? "17040000000000003" : navId;
		model.addAttribute("navId", navId);
		return "/sys/index";
	}
	
	/**
	 * 无权限页面
	 */
	@RequestMapping(value = "/403")
	public String _403(HttpServletRequest request, HttpServletResponse response) {
		return _403;
	}
}
